package com.sptus.web.quartz.job;

import cn.hutool.core.util.ObjectUtil;
import com.sptus.web.service.impl.CloudEquipmentServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.*;
@Service
@Slf4j
public class SptHomePowerGenerationGapacity  {
    @Autowired
    CloudEquipmentServiceImpl equipmentService;

    //电池用量数据统计 每30分钟更新一次
    @PostConstruct
    @Scheduled(cron = "0 1/30 * * * ?")
    public void execute() throws JobExecutionException {
       ArrayList<HashMap<String,Object>> arrayList= equipmentService.getEquipmentGenration();
        Map<String, ArrayList<HashMap<String, Object>>> groupedMap = new HashMap<>();

        for (HashMap<String, Object> map : arrayList) {
            String key = map.get("battery_number").toString();
            if (!groupedMap.containsKey(key)) {
                groupedMap.put(key, new ArrayList<>());
            }
            groupedMap.get(key).add(map);
        }

        Set<String> strings = groupedMap.keySet();
        for (String string : strings) {
            ArrayList<HashMap<String, Object>> objList = groupedMap.get(string);
            HashMap<String,Object> devBG=new HashMap<>();
            int[]  H00_23 = new int[24];
            int[]   D01_31 =new int[31];
            int[]  M01_12 =new  int[12] ;



            for (HashMap<String, Object> stringObjectHashMap : objList) {
                if(ObjectUtil.isEmpty(stringObjectHashMap.get("H00_23"))){
                    continue;
                }
                String[] split = stringObjectHashMap.get("H00_23").toString().split(",");
                for (int i = 0; i < split.length; i++) {
                    H00_23[i]=((( Integer.parseInt(split[i]))+H00_23[i]));
                }

                String[] split1 = stringObjectHashMap.get("D01_31").toString().split(",");
                for (int i = 0; i < split1.length; i++) {
                    D01_31[i]=((( Integer.parseInt(split1[i]))+D01_31[i]));
                }

                String[] split2 = stringObjectHashMap.get("M01_12").toString().split(",");
                for (int i = 0; i < split2.length; i++) {
                    M01_12[i]=((( Integer.parseInt(split2[i]))+M01_12[i]));
                }
            }
            devBG.put("day",Arrays.toString(H00_23).replace("[", "").replace("]",""));
            devBG.put("month",Arrays.toString(D01_31).replace("[", "").replace("]",""));
            devBG.put("year",Arrays.toString(M01_12).replace("[", "").replace("]",""));
            if(string.equals("0")){
                devBG.put("id","0");
                devBG.put("devType","0");
            }else
            if(string.equals("1")){
                devBG.put("id","1");
                devBG.put("devType","1");
            }
            else
                //3800A
            if(string.equals("2")){
                devBG.put("id","1");
                devBG.put("devType","1");
                equipmentService.saveGapacity(devBG);
            }
            else
                //3800B
            if(string.equals("3")){
                devBG.put("id","2");
                devBG.put("devType","2");
                equipmentService.saveGapacity(devBG);
            }
            else
                ////3800C
            if(string.equals("4")){
                devBG.put("id","3");
                devBG.put("devType","3");
                equipmentService.saveGapacity(devBG);
            }


        }


    }
}

